//
//  BL_IssuView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/11.
//

#import "BL_IssuView.h"

@interface BL_IssuView ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UILabel *ploLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewBottom;
@property (weak, nonatomic) IBOutlet UIView *baseView;

@end

@implementation BL_IssuView


+(void)showIssuView{
    BL_IssuView * view = [[[NSBundle mainBundle]loadNibNamed:@"BL_IssuView" owner:nil options:nil]lastObject];
    view.frame = BL_WINDOW_FRAME;
    [BL_WINDOW addSubview:view];
}


-(void)awakeFromNib{
    [super awakeFromNib];


    self.cancleBtn.layer.cornerRadius = 8.75;
    self.cancleBtn.layer.masksToBounds = YES;
    
    self.commitBtn.layer.cornerRadius = 8.75;
    self.commitBtn.layer.masksToBounds = YES;

    
    [BLTool viewBeizerRect:self.baseView.bounds view:self.baseView corner:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    
    self.contentView.layer.cornerRadius = 5;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.borderWidth = 1;
    self.contentView.layer.borderColor = [UIColor chColorWithHexString:@"#E7E8FF"].CGColor;
    
    self.contentView.delegate = self;
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    self.backgroundColor = BL_COLOR_ALPHA;
    
    
}
- (IBAction)disMissView:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (IBAction)commitFeedback:(UIButton *)sender {
    
    if (self.contentView.text.length <= 0) {
        [HUD showtip:@"内容为空不能提交"];
        return;
    }
    
    NSString * feedback = self.contentView.text;
    NSDictionary * fe  = @{@"suggestion":feedback};
    
    [BLHttpTool postWithPath:BL_USER_FEEDBACK params:fe successBlock:^(NSDictionary * _Nonnull obj) {
        [HUD showtip:[obj objectForKey:@"content"]];
        
        [self disMissView:nil];
        
    } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
       
        [self disMissView:nil];
        [HUD showtip:@"提交失败"];
//        NSLog(@"提交失败%@",error);
    }];
}




//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = aNotification.userInfo;
    CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];//键盘动画结束时的frame
    NSTimeInterval timeInterval = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];//动画持续时间
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];//动画曲线类型
    //your animation code
    __weak typeof(self) weakself = self;
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:timeInterval animations:^{
        self.viewBottom.constant = frame.size.height;
    }];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    self.viewBottom.constant = 0;
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
//    self.ploLabel.hidden = YES;
}

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.ploLabel.hidden = NO;
    }else{
        self.ploLabel.hidden = YES;
    }
}

@end
